﻿using Common;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace SFJDManagement
{
    /// <summary>
    /// Page_Function.xaml 的交互逻辑
    /// </summary>
    public partial class Page_Function : Page
    {
        public Grid layout_root = new Grid();
        public WrapPanel layout = new WrapPanel();
        private Dictionary<string, Button> dictionary = new Dictionary<string, Button>(10);
        public static Page_Function instance;

        public Page_Function()
        {
            InitializeComponent();
            IniDictionary();
            layout.VerticalAlignment = VerticalAlignment.Center;
            layout.HorizontalAlignment = HorizontalAlignment.Center;
            FontSize = 20;
            Foreground = Brushes.Red;
            VerticalAlignment = VerticalAlignment.Center;
            HorizontalAlignment = HorizontalAlignment.Center;
            Ini();
            Content = layout;
        }

        private void Ini()
        {
            if (Runtime.config.IsAdmin)
            {
                layout.Children.Add(dictionary["查询"]);
                layout.Children.Add(dictionary["通讯录"]);
                layout.Children.Add(dictionary["划号"]);
                layout.Children.Add(dictionary["台账"]);
                layout.Children.Add(dictionary["编辑委托人"]);
                layout.Children.Add(dictionary["编辑联系人"]);
                layout.Children.Add(dictionary["编辑用户"]);
                layout.Children.Add(dictionary["编辑数据"]);
                layout.Children.Add(dictionary["软件升级"]);
            }
            else
            {
                if (Runtime.config.UserLicense == License.Guest)
                {
                    layout.Children.Add(dictionary["查询"]);
                    layout.Children.Add(dictionary["通讯录"]);
                    layout.Children.Add(dictionary["预登记"]);
                }
                else if (Runtime.config.UserLicense == License.User)
                {
                    layout.Children.Add(dictionary["查询"]);
                    layout.Children.Add(dictionary["通讯录"]);
                    layout.Children.Add(dictionary["划号"]);
                }
                else if (Runtime.config.UserLicense == License.Archive)
                {
                    layout.Children.Add(dictionary["查询"]);
                    layout.Children.Add(dictionary["通讯录"]);
                    layout.Children.Add(dictionary["台账"]);
                    layout.Children.Add(dictionary["预登记"]);
                }
            }
            //layout.Children.Add(dictionary["启动服务"]);
            //layout.Children.Add(dictionary["调试"]);
        }

        private void IniDictionary()
        {
            Button button1 = GetButton("查询");
            button1.Click += Button1_Click;
            Button button2 = GetButton("划号");
            button2.Click += Button2_Click;
            Button button3 = GetButton("台账");
            button3.Click += Button3_Click;
            Button button4 = GetButton("预登记");
            Button button5 = GetButton("发送");
            Button button6 = GetButton("编辑委托人");
            button6.Click += Button6_Click;
            Button button7 = GetButton("编辑联系人");
            button7.Click += Button7_Click;
            Button button8 = GetButton("编辑用户");
            button8.Click += Button8_Click;
            Button button9 = GetButton("编辑数据");
            button9.Click += Button9_Click;
            Button button10 = GetButton("软件升级");
            button10.Click += Button10_Click;
            Button button11 = GetButton("启动服务");
            button11.Click += Button11_Click;
            Button button12 = GetButton("通讯录");
            button12.Click += Button12_Click;
            Button button13 = GetButton("调试");
            button13.Click += Button13_Click;

            dictionary.Add(button1.Content.ToString(), button1);
            dictionary.Add(button2.Content.ToString(), button2);
            dictionary.Add(button3.Content.ToString(), button3);
            dictionary.Add(button4.Content.ToString(), button4);
            dictionary.Add(button5.Content.ToString(), button5);
            dictionary.Add(button6.Content.ToString(), button6);
            dictionary.Add(button7.Content.ToString(), button7);
            dictionary.Add(button8.Content.ToString(), button8);
            dictionary.Add(button9.Content.ToString(), button9);
            dictionary.Add(button10.Content.ToString(), button10);
            dictionary.Add(button11.Content.ToString(), button11);
            dictionary.Add(button12.Content.ToString(), button12);
            dictionary.Add(button13.Content.ToString(), button13);
        }

        private void Button13_Click(object sender, RoutedEventArgs e)
        {
            if(new ServiceHelper().StopService("SFJDService"))
            {
                Process[] pro = Process.GetProcesses();//获取已开启的所有进程
                for (int i = 0; i < pro.Length; i++)//遍历所有查找到的进程
                {
                    //判断此进程是否是要查找的进程
                    if (pro[i].ProcessName.ToString().ToLower() == "sfjdservice")
                    {
                        pro[i].Kill();//结束进程
                    }
                }
                //new SFJDService.SFJDService().Debug();
                Process p = new Process();
                p.StartInfo.FileName = Runtime.programPath +  "\\SFJD_Debug.exe";
                p.Start();
            }
        }

        private void Button12_Click(object sender, RoutedEventArgs e)
        {
            //Win_Main2.instance.Layout_Function.Content = null;
            //Page_PhoneBook book = new Page_PhoneBook();
            //Win_Main2.instance.Layout_Function.Navigate(new Uri("Page_PhoneBook.xaml"), UriKind.Relative);
            Win_Main2.instance.Layout_Function.Content = new Page_PhoneBook();
            //Win_Main2.instance.Layout_Function.Content = new Win_Main_1();
        }

        private void Button11_Click(object sender, RoutedEventArgs e)
        {
            ServiceHelper server = new ServiceHelper();
            if (!server.IsServiceStart(Runtime.serviceName))
            {
                try
                {
                    server.StartService(Runtime.serviceName);
                }
                catch(Exception ex)
                {
                    Content = "服务不能启动...\r\n" + ex.InnerException.Message;
                    return;
                }
            }
            else
            {
                Content = "服务已经启动...";
            }
        }

        private void Button10_Click(object sender, RoutedEventArgs e)
        {
            Win_Main2.instance.Layout_Function.Content = new Page_UpdateProgram();
        }

        private void Button9_Click(object sender, RoutedEventArgs e)
        {
            Win_Main2.instance.Layout_Function.Content = new Page_Edit_Data();
        }

        private void Button8_Click(object sender, RoutedEventArgs e)
        {
            Win_Main2.instance.Layout_Function.Content = new Page_Edit_Users();
        }

        private void Button7_Click(object sender, RoutedEventArgs e)
        {
            Win_Main2.instance.Layout_Function.Content = new Page_Edit_Contact();
        }

        private void Button6_Click(object sender, RoutedEventArgs e)
        {
            Win_Main2.instance.Layout_Function.Content = new Page_Edit_Brigade();
        }

        private void Button3_Click(object sender, RoutedEventArgs e)
        {
            new Win_Account(Win_Main2.instance);
        }

        private void Button2_Click(object sender, RoutedEventArgs e)
        {
            Win_Main2.instance.Layout_Function.Content = new Page_Register();
        }

        private void Button1_Click(object sender, RoutedEventArgs e)
        {
            Win_Ledger winLedger = new Win_Ledger(Win_Main2.instance);
            winLedger.Owner = Win_Main2.instance;
        }

        private Button GetButton(string name)
        {
            Button b = new Button() {
                Content = name,
                FontSize = 16,
                Width = 100,
                Height = 100,
                Background = Brushes.AliceBlue,
                Margin = new Thickness(10),
            };
            return b;
        }

        protected void Reset()
        {
            layout.Children.Clear();
            Ini();
        }
    }
}
